﻿using COMOSOFT.Class;
using MSoft.Shipping.Control;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using System.Data.SqlClient;

namespace COMOSOFT.Production.Forms
{
    public partial class FrmOrderInfo : Form
    {
        Task tak;
        ClassShare cls = new ClassShare("FrmOrderInfo");
        int FLID;
        string Item;
        public FrmOrderInfo()
        {
            InitializeComponent();
        }

        #region [LoadData]
        public void LoadData()
        {
            try
            {
                if (tak == null || tak.Status != TaskStatus.Running)
                {                   
                    splashScreenManager1.ShowWaitForm();
                    gridControl_OrderInfo.DataSource = null;
                    tak = Task.Factory.StartNew(() => LoadGaneral());
                    tak.ContinueWith(tt => FillTable());                    
                    new BandedCellSelectionHelper(bandedGridView_OrderInfo);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }
        private void LoadGaneral()
        {
            sp_ProOrderListTableAdapter.Fill(this.dataSet_Production.sp_ProOrderList);
        }

        private delegate void dlgFillTable();
        private void FillTable()
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new dlgFillTable(FillTable));
                }
                else
                {
                    gridControl_OrderInfo.DataSource = dataSet_Production.sp_ProOrderList;
                    splashScreenManager1.CloseWaitForm();
                }
            }
            catch
            {
                MessageBox.Show("Progess fail");
            }
        }
        #endregion

        private void dataNavigator_ProductionPlanning_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Add")
            {
                try
                {
                    GridView view = gridView_AddPP as GridView;
                    for (int i = 0; i < view.RowCount; i++)
                    {
                        cls.UpdateData("sp_ProductionPlanningInsert", new string[] { "@year", "@month", "@OID", "@SubIID", "@SubFLID", "@AssignedQty", "@CreateBy", "@AssignedTo", "@IsAssigned" }
                                                                    , new object[] {
                                                                                      Convert.ToInt32(view.GetDataRow(i)["Year"]),
                                                                                      Convert.ToInt32(view.GetDataRow(i)["Month"]),
                                                                                      Convert.ToInt32(view.GetDataRow(i)["OID"]),
                                                                                      Convert.ToInt16(view.GetDataRow(i)["SubItem"]),
                                                                                      Convert.ToInt16(view.GetDataRow(i)["SubFactory"]),
                                                                                      Convert.ToInt16(view.GetDataRow(i)["AssignedQty"]),
                                                                                      view.GetDataRow(i)["CreateBy"].ToString(),
                                                                                      view.GetDataRow(i)["AssignedTo"].ToString(),
                                                                                      Convert.ToBoolean(view.GetDataRow(i)["IsAssigned"])
                                                                                   });
                    }
                    dockPanel_AddPP.Close();
                    LoadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                dockPanel_AddPP.Close();
            }
        }

        private void addProdcutionPlanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view = bandedGridView_OrderInfo as GridView;
                dockPanel_AddPP.Show();
                string item = null;
                int[] arritem = view.GetSelectedRows();
                foreach (int index in arritem)
                {
                    item += view.GetDataRow(index)["OID"].ToString() + ",";                   
                }
                sp_ProAddProductionPlanningTableAdapter.Fill(dataSet_Production.sp_ProAddProductionPlanning, item);
                employeeTableAdapter.Fill(dataSet_Production.Employee,ClassShare.Usid);
                repositoryItemComboBox_Year.Items.Clear();          
                for (int y = DateTime.Now.Year; y <= DateTime.Now.Year+3; y++)
                {
                    repositoryItemComboBox_Year.Items.Add(y);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region[Item]
        private void repositoryItemLookUpEdit_SubItem_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Item = gridView_AddPP.GetFocusedDataRow()["Item"].ToString();
            if (e.Button.Tag.ToString() == "Add")
            {
                dockPanel_SubItem.Show();
            }
            else
            {                
                subItemTableAdapter.Fill(this.dataSet_Production.SubItem,Item); 
            }            
        }

        private void bnt_ItemCancel_Click(object sender, EventArgs e)
        {
            dockPanel_SubItem.Close();
        }

        private void bnt_ItemOK_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = cls.connection();
                SqlCommand com = new SqlCommand("insert into SubItem (Item,SubItem) values (@Item,@SubItem)", con);
                com.Parameters.AddWithValue("@Item", Item);
                com.Parameters.AddWithValue("@SubItem", txt_ItemName.EditValue.ToString());
                com.ExecuteNonQuery();
                dockPanel_SubItem.Close();
                subItemTableAdapter.Fill(dataSet_Production.SubItem, Item);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[SubFactory]
        private void repositoryItemLookUpEdit_SubFactory_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FLID = Convert.ToInt32(gridView_AddPP.GetFocusedDataRow()["FLID"]);
            if (e.Button.Tag.ToString() == "Add")
            {
                dockPanel_SubFactory.Show();
            }
            else
            {               
                subFactoryTableAdapter.Fill(this.dataSet_Production.SubFactory, FLID);
            }
        }
        private void bnt_FctCancel_Click(object sender, EventArgs e)
        {
            dockPanel_SubFactory.Close();
        }

        private void bnt_FctOK_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = cls.connection();
                SqlCommand com = new SqlCommand("insert into SubFactory (FLID,SubFactoryName) values (@FLID,@FctName)", con);
                com.Parameters.AddWithValue("@FLID", FLID);
                com.Parameters.AddWithValue("@FctName", txt_FctName.EditValue.ToString());
                com.ExecuteNonQuery();
                dockPanel_SubFactory.Close();
                subFactoryTableAdapter.Fill(dataSet_Production.SubFactory, FLID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion       

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void saveLayoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
